package com.designoutput.ui.toolGroup
{
	import com.designoutput.enum.ToolType;
	import com.designoutput.icon.*;
	import com.designoutput.manager.ResourceManager;
	import com.designoutput.ui.LeftToolBar;
	import com.designoutput.ui.controls.BoxSplit;
	import com.designoutput.ui.controls.ToolButton;
	import com.designoutput.utils.Globals;
	import com.designoutput.utils.SnapUtils;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class BasicToolGroup extends Sprite
	{
		internal var leftPanel:LeftToolBar;
		internal var ancestor:DesignoutputCard;
		internal var selection:ToolButton;
		internal var trash:ToolButton;
		internal var constrainBtn:ToolButton;
		internal var at:ToolButton;

		public function BasicToolGroup(leftPanel:LeftToolBar)
		{
			this.leftPanel = leftPanel;
			this.ancestor = this.leftPanel.p;
			build();
		}
		
		private function build():void{
			selection=new ToolButton(ToolType.None, new SelectionIcon(0, 0), 8, 4);
			selection.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			selection.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			selection.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			selection.x=10;
			selection.y=22;
			selection.Mark(true);
			this.addChild(selection);
			
			var rectangle:ToolButton=new ToolButton(ToolType.Rectangle, new RectangleIcon(0, 0), 5, 5);
			rectangle.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			rectangle.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			rectangle.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			rectangle.x=40;
			rectangle.y=22;
			this.addChild(rectangle);
			
			var roundRectangle:ToolButton=new ToolButton(ToolType.RoundRectangle, new RoundRectangleIcon(0, 0), 5, 5);
			roundRectangle.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			roundRectangle.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			roundRectangle.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			roundRectangle.x=70;
			roundRectangle.y=22;
			this.addChild(roundRectangle);
			
			var ellipse:ToolButton=new ToolButton(ToolType.Ellipse, new EllipseIcon(0, 0), 5, 5);
			ellipse.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			ellipse.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			ellipse.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			ellipse.x=100;
			ellipse.y=22;
			this.addChild(ellipse);
			
			/********************************************/
			var starPolygon:ToolButton=new ToolButton(ToolType.StarPolygon, new StarIcon(0, 0), 1, 1);
			starPolygon.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			starPolygon.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			starPolygon.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			starPolygon.x=10;
			starPolygon.y=52;
			this.addChild(starPolygon);
			
			var freeFormLine:ToolButton=new ToolButton(ToolType.FreeFormLine, new FreeFormLineIcon(0, 0), 7, 7);
			freeFormLine.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			freeFormLine.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			freeFormLine.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			freeFormLine.x=40;
			freeFormLine.y=52;
			this.addChild(freeFormLine);
			
			var line:ToolButton=new ToolButton(ToolType.Line, new LineIcon(0, 0), 7, 7);
			line.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			line.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			line.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			line.x=70;
			line.y=52;
			this.addChild(line);
			
			var text:ToolButton=new ToolButton(ToolType.Text, new TextIcon(0, 0), 5, 5);
			text.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			text.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			text.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			text.x=100;
			text.y=52;
			this.addChild(text);
			
			
			/********************************************/
			var image:ToolButton=new ToolButton(ToolType.Image, new ImageIcon(0, 0), 6, 6);
			image.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			image.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			image.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			image.x=10;
			image.y=82;
			this.addChild(image);
			
			// delete button
			trash=new ToolButton(ToolType.Trash, new TrashIcon(0, 0), 7, 7);
			trash.addEventListener(MouseEvent.MOUSE_DOWN, this.trashMouseDown);
			trash.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			trash.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			trash.x=40;
			trash.y=82;
			this.addChild(trash);
			
			// size constrain
			constrainBtn=new ToolButton(ToolType.Constrain, new ConstrainIcon(0, 0), 6, 6);
			constrainBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			constrainBtn.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			constrainBtn.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			constrainBtn.x=70;
			constrainBtn.y=82;
			this.addChild(constrainBtn);
			
			var margin:ToolButton=new ToolButton(ToolType.Margin, new MarginIcon(0, 0), 4, 4);
			margin.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			margin.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			margin.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			margin.x=100;
			margin.y=82;
			margin.Mark(true);
			this.addChild(margin);
			
			/********************************************/
			
			var ruler:ToolButton=new ToolButton(ToolType.Ruler, new RulerIcon(0, 0), 4, 4);
			ruler.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			ruler.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			ruler.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			ruler.x=10;
			ruler.y=112;
			ruler.Mark(true);
			this.addChild(ruler);
			
			var eraser:ToolButton=new ToolButton(ToolType.Eraser, new EraserIcon(0, 0), 4, 4);
			eraser.addEventListener(MouseEvent.MOUSE_DOWN, this.ToolMouseDown);
			eraser.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			eraser.addEventListener(MouseEvent.MOUSE_OUT, this.ancestor.hover.hideEvent);
			eraser.x=40;
			eraser.y=112;
			eraser.visible=true;
			this.addChild(eraser);
			
			var split:BoxSplit=new BoxSplit(BoxSplit.MEDIUMTYPE);
			split.x=20;
			split.y=150;
			this.addChild(split);
		}
		
		public function setDefaultTool():void
		{
			if (this.at != null)
			{
				this.at.Mark(false);
			}
			this.at=this.selection;
			this.at.Mark(true);
			this.ancestor.setTool(this.at.type);
		}
		
		private function ToolMouseDown(evt:MouseEvent):void
		{
			var toolButton:ToolButton=evt.currentTarget as ToolButton;
			switch (toolButton.type)
			{
				case ToolType.Ruler:
					this.ancestor.command.showRuler(!toolButton.isMarked());
					toolButton.Mark(!toolButton.isMarked());
					break;
				case ToolType.Margin:
					this.ancestor.command.showCardMargins(!toolButton.isMarked());
					toolButton.Mark(!toolButton.isMarked());
					break;
				case ToolType.Constrain:
					SnapUtils.snapEnabled = !toolButton.isMarked();
					toolButton.Mark(!toolButton.isMarked());
					break;
				default:
					if (this.at != null)
					{
						this.at.Mark(false);
					}
					this.at=evt.currentTarget as ToolButton;
					this.at.Mark(true);
					this.ancestor.setTool(this.at.type);
					break;
			}
		}
		
		private function trashMouseDown(evt:MouseEvent):void
		{
			this.trash.Mark(true);
			this.trash.addEventListener(MouseEvent.MOUSE_UP, trashMouseUp);
			
			this.ancestor.command.deleteElements();
		}
		
		private function trashMouseUp(evt:MouseEvent):void
		{
			this.trash.Mark(false);
		}
		
		/**
		 * Called when SHIFT key onpress or up
		 * @param flag:Boolean
		 * 
		 */		
		public function constrainSetMarked(flag:Boolean):void{
			if(flag){
				if(!this.constrainBtn.isMarked()){
					this.constrainBtn.Mark(flag);
				}
			}else{
				if(this.constrainBtn.isMarked()){
					this.constrainBtn.Mark(flag);
				}
			}
		}
		
		private function ToolMouseOver(evt:MouseEvent):void
		{
			var type:String=(evt.currentTarget as ToolButton).type;
			switch (type)
			{
				case ToolType.None:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_select"));
					break;
				case ToolType.Rectangle:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_rectangle"));
					break;
				case ToolType.RoundRectangle:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_roundrectangle"));
					break;
				case ToolType.Ellipse:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_ellipse"));
					break;
				case ToolType.StarPolygon:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_starpolygon"));
					break;
				case ToolType.FreeFormLine:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_freeformline"));
					break;
				case ToolType.Line:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_line"));
					break;
				case ToolType.Text:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_text"));
					break;
				case ToolType.Image:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_image"));
					break;
				case ToolType.Trash:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_trash"));
					break;
				case ToolType.Constrain:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_constrain"));
					break;
				case ToolType.Margin:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_margins"));
					break;
				case ToolType.Ruler:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_ruler"));
					break;
				case ToolType.Eraser:
					this.ancestor.hover.setText(ResourceManager.getValue("rm_eraser"));
					break;
				default:
					break;
			}
		}
	}
}